from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v136.fed_cm import click_dialog_button
from selenium.webdriver.support.expected_conditions import element_to_be_clickable
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from urllib.parse import unquote, urlparse, parse_qs
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
import requests
import os
import time
import random


#有分页滚动刷新网页爬虫
os.makedirs('vcg_photos_2', exist_ok=True)

# 设置 Edge 浏览器选项
edge_options = Options()
edge_options.add_argument('--headless')  # 无头模式
edge_options.add_argument('--disable-gpu')
edge_options.add_argument('--no-sandbox')
edge_options.add_argument('--disable-dev-shm-usage')
edge_options.add_argument('--start-maximized')  # 最大化窗口确保所有元素可见
edge_options.add_argument('--log-level=3')  # 减少日志输出

# 初始化 Edge WebDriver
driver = webdriver.Edge()

# 从原始URL解析实际图片地址
def extract_image_url(original_url):
    parsed_url = urlparse(original_url)
    query_params = parse_qs(parsed_url.query)
    objurl = query_params.get('mediaurl', [''])[0]
    return unquote(objurl)  # 解码URL编码


# 下载图片
def download_image(url, save_path):
    response = requests.get(url, stream=True)
    if response.status_code == 200:
        with open(save_path, 'wb') as f:
            for chunk in response.iter_content(1024):
                f.write(chunk)
        print(f"图片已保存至: {save_path}")
    else:
        print("下载失败，状态码:", response.status_code)


def multi_page_download(target_address,page):
    current_page = 0
    current_photo = 0
    while current_page < page:
        # 设置请求头
        driver.execute_cdp_cmd('Network.setUserAgentOverride', {
            "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59"
        })

        print(f"正在访问 {target_address}...{page}")

        # 打开目标网页

        time.sleep(3)

        # 模拟滚动加载更多图片
        last_height = driver.execute_script("return document.body.scrollHeight")
        scroll_attempts = 0
        print("开始滚动加载图片...")

        while scroll_attempts < 5:  # 最多滚动200次
            # 滚动到页面底部
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

            # 随机等待时间（更人性化）
            time.sleep(random.uniform(1.5, 3.5))

            # 检查页面高度是否变化
            new_height = driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
                break
            last_height = new_height
            scroll_attempts += 1
            print(f"已滚动 {scroll_attempts} 次，当前高度: {new_height}px")

        image_containers = driver.find_elements(By.CSS_SELECTOR, 'figure[class="galleryItem"]')
        print(f"找到 {len(image_containers)} 张图片")

        for idx, container in enumerate(image_containers):
            try:
                img_element = container.find_element(By.CSS_SELECTOR, 'img')

                img_source_url = "https:" + img_element.get_attribute('data-src')
                print("解析出的图片地址:", img_source_url)

                save_path = f"vcg_photos_2/empty_room_{idx + current_photo + 1}.jpg"
                download_image(img_source_url, save_path)
            except Exception as e:
                print(f"下载第 {idx + 1} 张图片时出错: {str(e)}")

        current_photo += len(image_containers)
        # 先找到下一页按键子元素
        child_element = driver.find_element(By.CSS_SELECTOR, 'span[class="paginationRightButton"]')

        # 然后获取其直接父元素
        parent_element = child_element.find_element(By.XPATH, './parent::span')
        parent_element.click()
        current_page += 1


if __name__ == "__main__":
    target_address = "https://www.vcg.com/creative-image/kongfangjian/?page=60"
    driver.get(target_address)
    multi_page_download(target_address, 50)
